package com.nbgroup.web;

import com.nbgroup.entity.Appraise;
import com.nbgroup.entity.region.Province;
import com.nbgroup.service.Appraise.AppraiseService;
import com.nbgroup.service.Identity.IdentityService;
import com.nbgroup.utils.TimeTools;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 统计控制器
 *
 * @author liukai
 */
@RestController
@RequestMapping(value = "/statistics")
public class StatisticsController {

    private IdentityService identityService;

    @Autowired
    AppraiseService appraiseService;
    @Autowired
    public StatisticsController(IdentityService identityService) {
        this.identityService = identityService;
    }

    private String getStartTime(String time){
        if (time.equals("1")){
            return TimeTools.getTimeDate()+" 00:00:00";
        }
        if (time.equals("2")){
            return TimeTools.formatDate(TimeTools.getThisWeekDate(1))+" 00:00:00";
        }
        if (time.equals("3")){
            return TimeTools.getFirstDay()+" 00:00:00";
        }
        if (time.equals("4")){
            return TimeTools.getThisYear()+"-01-01 00:00:00";
        }
        if (time.equals("0")){
            return "";
        }
        return "";
    }
    private String getEndTime(String time){
        if (time.equals("1")){
            return TimeTools.getTimeDate()+" 23:59:59";
        }
        if (time.equals("2")){
            return TimeTools.formatDate(TimeTools.getThisWeekDate(7))+" 23:59:59";
        }
        if (time.equals("3")){
            return TimeTools.getEndDay()+" 23:59:59";
        }
        if (time.equals("4")){
            return TimeTools.getThisYear()+"-12-31 23:59:59";
        }
        if (time.equals("0")){
            return "";
        }
        return "";
    }

    /**
     * 性别占比
     *
     * @author liukai
     */
    @GetMapping(value = "/sex/{time}/{startdate}/{enddate}")
    public Map sex(@PathVariable String time,@PathVariable String startdate,@PathVariable String enddate) {
        Map<String, Object> map = new HashMap<>(16);
        if(!("0".equals(startdate)&&"0".equals(enddate))){
            String startTime = startdate;
            String endTime = enddate;
            Long manCount = identityService.countByTime("1",startTime,endTime);
            Long womanCount = identityService.countByTime("2",startTime,endTime);
            map.put("manCount", manCount);
            map.put("womanCount", womanCount);
            return map;
        }else
        if(!("0".equals(enddate))){
            String startTime = "2010-01-01 00:00:00";
            String endTime = enddate;
            Long manCount = identityService.countByTime("1",startTime,endTime);
            Long womanCount = identityService.countByTime("2",startTime,endTime);
            map.put("manCount", manCount);
            map.put("womanCount", womanCount);
            return map;
        }else
        if(!("0".equals(startdate))){
            String startTime = startdate;
            String endTime = "2099-12-31 23:59:59";
            Long manCount = identityService.countByTime("1",startTime,endTime);
            Long womanCount = identityService.countByTime("2",startTime,endTime);
            map.put("manCount", manCount);
            map.put("womanCount", womanCount);
            return map;
        }
        if(time.equals("0")){
            Long manCount = identityService.count("1");
            Long womanCount = identityService.count("2");
            map.put("manCount", manCount);
            map.put("womanCount", womanCount);
        }else {
            String startTime = getStartTime(time);
            String endTime = getEndTime(time);
            Long manCount = identityService.countByTime("1",startTime,endTime);
            Long womanCount = identityService.countByTime("2",startTime,endTime);
            map.put("manCount", manCount);
            map.put("womanCount", womanCount);
        }
        return map;
    }

    /**
     * 年龄占比
     *
     * @author liukai
     */
    @RequestMapping(value = "/age2")
    public List<Long> age2() {
        List<Long> list = new ArrayList<>();
        for (int i = 10; i <= 100; i += 10) {
            list.add(identityService.count(i - 10, i));
        }
        return list;
    }
    /**
     * 年龄占比
     *
     * @author liukai
     */
    @GetMapping(value = "/age/{time}/{startdate}/{enddate}")
    public List<Long> age(@PathVariable String time,@PathVariable String startdate,@PathVariable String enddate) {
        List<Long> list = new ArrayList<>();
        if(!("0".equals(startdate)&&"0".equals(enddate))){
            String startTime = startdate;
            String endTime = enddate;
            for (int i = 10; i <= 100; i += 10) {
                list.add(identityService.countByTime(i - 10, i,startTime,endTime));
            }
            return list;
        }else
        if(!("0".equals(enddate))){
            String startTime = "2010-01-01 00:00:00";
            String endTime = enddate;
            for (int i = 10; i <= 100; i += 10) {
                list.add(identityService.countByTime(i - 10, i,startTime,endTime));
            }
            return list;
        }else
        if(!("0".equals(startdate))){
            String startTime = startdate;
            String endTime = "2099-12-31 23:59:59";
            for (int i = 10; i <= 100; i += 10) {
                list.add(identityService.countByTime(i - 10, i,startTime,endTime));
            }
            return list;
        }
        if(time.equals("0")){
            for (int i = 10; i <= 100; i += 10) {
                list.add(identityService.count(i - 10, i));
            }
        }else {
            String startTime = getStartTime(time);
            String endTime = getEndTime(time);
            for (int i = 10; i <= 100; i += 10) {
                list.add(identityService.countByTime(i - 10, i,startTime,endTime));
            }
        }
        return list;
    }

    /**
     * 各省数据
     *
     * @param
     * @author 顾志杰
     * @date 2018/8/19 13:54
     */
    @GetMapping(value = "/province/{time}/{startdate}/{enddate}")
    public List<Province> province(@PathVariable String time,@PathVariable String startdate,@PathVariable String enddate) {
        if(!("0".equals(startdate)&&"0".equals(enddate))){
            String startTime = startdate;
            String endTime = enddate;
            return identityService.findProvinceCountByTime(startTime,endTime);
        }else
        if(!("0".equals(enddate))){
            String startTime = "2010-01-01 00:00:00";
            String endTime = enddate;
            return identityService.findProvinceCountByTime(startTime,endTime);
        }else
        if(!("0".equals(startdate))){
            String startTime = startdate;
            String endTime = "2099-12-31 23:59:59";
            return identityService.findProvinceCountByTime(startTime,endTime);
        }

        if(time.equals("0")){
            return identityService.findProvinceCount();
        }else {
            String startTime = getStartTime(time);
            String endTime = getEndTime(time);
            return identityService.findProvinceCountByTime(startTime,endTime);
        }
    }

    @GetMapping(value = "/findAppraiseList/{time}/{startdate}/{enddate}")
    public List<Appraise> findAppraiseList(@PathVariable String time,@PathVariable String startdate,@PathVariable String enddate) {
        if(!("0".equals(startdate)&&"0".equals(enddate))){
            String startTime = startdate;
            String endTime = enddate;
            return appraiseService.getAppraiseForScreenNewByTime(startTime,endTime);
        }else
        if(!("0".equals(enddate))){
            String startTime = "2010-01-01 00:00:00";
            String endTime = enddate;
            return appraiseService.getAppraiseForScreenNewByTime(startTime,endTime);
        }else
        if(!("0".equals(startdate))){
            String startTime = startdate;
            String endTime = "2099-12-31 23:59:59";
            return appraiseService.getAppraiseForScreenNewByTime(startTime,endTime);
        }

        if(time.equals("0")){
            return appraiseService.getAppraiseForScreenNew();
        }else {
            String startTime = getStartTime(time);
            String endTime = getEndTime(time);
            return appraiseService.getAppraiseForScreenNewByTime(startTime,endTime);
        }

    }

    /**
     * 未来一个月
     *
     * @param
     * @author 顾志杰
     * @date 2018/8/19 13:55
     */
    @GetMapping(value = "/futureOneMonth/{time}/{startdate}/{enddate}")
    public long[] futureOneMonth(@PathVariable String time) {
        return identityService.findFutureOneMonthCount();
    }

    /**
     * 月客流量
     *
     * @param
     * @author 顾志杰
     * @date 2018/8/20 9:45
     */
    @GetMapping(value = "/current/{time}/{startdate}/{enddate}")
    public Long current(@PathVariable String time,@PathVariable String startdate,@PathVariable String enddate) {

        if(!("0".equals(startdate)&&"0".equals(enddate))){
            String startTime = startdate;
            String endTime = enddate;
            return identityService.findMonthCountByTime(startTime,endTime);
        }else
        if(!("0".equals(enddate))){
            String startTime = "2010-01-01 00:00:00";
            String endTime = enddate;
            return identityService.findMonthCountByTime(startTime,endTime);
        }else
        if(!("0".equals(startdate))){
            String startTime = startdate;
            String endTime = "2099-12-31 23:59:59";
            return identityService.findMonthCountByTime(startTime,endTime);
        }
        if(time.equals("0")){
            return identityService.findMonthCount();
        }else {
            String startTime = getStartTime(time);
            String endTime = getEndTime(time);
            return identityService.findMonthCountByTime(startTime,endTime);
        }
    }

    @RequestMapping(value = "/current2")
    public Long current() {
        return identityService.findMonthCount();
    }

    /**
     * 统计分析 客流量统计
     * 2018年9月10日, PM 03:49:36
     *
     * @author 姜文
     */
    @GetMapping(value = "/passengerStatistics/{time}/{startdate}/{enddate}")
    public Map<String, Object> passengerStatistics(@PathVariable String time,@PathVariable String startdate,@PathVariable String enddate) {
        Map<String, Object> map = new HashMap<>();
        if(!("0".equals(startdate)&&"0".equals(enddate))){
            String startTime = startdate;
            String endTime = enddate;
            String[] provinceList = identityService.findProvinceStatisticsByTime(startTime,endTime);
            int[] manStrArray = identityService.findProvinceStatisticsForSexByTime("1",startTime,endTime);
            int[] womanStrArray = identityService.findProvinceStatisticsForSexByTime("2",startTime,endTime);
            map.put("province", provinceList);
            map.put("man", manStrArray);
            map.put("woman", womanStrArray);
            return map;
        }else
        if(!("0".equals(enddate))){
            String startTime = "2010-01-01 00:00:00";
            String endTime = enddate;
            String[] provinceList = identityService.findProvinceStatisticsByTime(startTime,endTime);
            int[] manStrArray = identityService.findProvinceStatisticsForSexByTime("1",startTime,endTime);
            int[] womanStrArray = identityService.findProvinceStatisticsForSexByTime("2",startTime,endTime);
            map.put("province", provinceList);
            map.put("man", manStrArray);
            map.put("woman", womanStrArray);
            return map;
        }else
        if(!("0".equals(startdate))){
            String startTime = startdate;
            String endTime = "2099-12-31 23:59:59";
            String[] provinceList = identityService.findProvinceStatisticsByTime(startTime,endTime);
            int[] manStrArray = identityService.findProvinceStatisticsForSexByTime("1",startTime,endTime);
            int[] womanStrArray = identityService.findProvinceStatisticsForSexByTime("2",startTime,endTime);
            map.put("province", provinceList);
            map.put("man", manStrArray);
            map.put("woman", womanStrArray);
            return map;
        }
        if(time.equals("0")){
            String[] provinceList = identityService.findProvinceStatistics();
            int[] manStrArray = identityService.findProvinceStatisticsForSex("1");
            int[] womanStrArray = identityService.findProvinceStatisticsForSex("2");
            map.put("province", provinceList);
            map.put("man", manStrArray);
            map.put("woman", womanStrArray);
        }else {
            String startTime = getStartTime(time);
            String endTime = getEndTime(time);
            String[] provinceList = identityService.findProvinceStatisticsByTime(startTime,endTime);
            int[] manStrArray = identityService.findProvinceStatisticsForSexByTime("1",startTime,endTime);
            int[] womanStrArray = identityService.findProvinceStatisticsForSexByTime("2",startTime,endTime);
            map.put("province", provinceList);
            map.put("man", manStrArray);
            map.put("woman", womanStrArray);
        }
        return map;
    }
}
